﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using Cat;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmUpdateUser : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcConnection con = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbcon = new DatabaseConnection();
        public frmUpdateUser()
        {
            InitializeComponent();
            txtContact_no.KeyPress += new KeyPressEventHandler(txtContact_no_KeyPress);
            txtName.KeyPress += new KeyPressEventHandler(txtName_KeyPress);
            txtUsername.Validating += new CancelEventHandler(txtUsername_Validating);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            btnClose.Click += new EventHandler(btnClose_Click);
            btnCheckAvailability.Click += new EventHandler(btnCheckAvailability_Click);
            txtEmail_Address.Validating += new CancelEventHandler(txtEmail_Address_Validating);
            btnView.Click += new EventHandler(btnView_Click);
        }

        void btnView_Click(object sender, EventArgs e)
        {
            frmListofuser frmL = new frmListofuser();
            frmL.Show();
            this.Hide();
        }
        void txtEmail_Address_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (txtEmail_Address.Text.Length > 0)
            {
                if (!rEMail.IsMatch(txtEmail_Address.Text))
                {
                    MessageBox.Show("Invalid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtEmail_Address.SelectAll();
                    e.Cancel = true;
                }
            }
        }
        void txtUsername_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9_]");
            if (txtUsername.Text.Length > 0)
            {
                if (!rEMail.IsMatch(txtUsername.Text))
                {
                    MessageBox.Show("Only letters,numbers and underscore is allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUsername.SelectAll();
                    e.Cancel = true;
                }
            }
        }
        void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space);
        }
        void btnCheckAvailability_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtUsername.Text == "")
                {
                    MessageBox.Show("Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUsername.Focus();
                    return;
                }
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "select Username from tbluserinfo where Username='" + txtUsername.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Username not available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (!rdr.Read())
                {
                    MessageBox.Show("Username available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtUsername.Focus();

                }
                if ((rdr != null))
                {
                    rdr.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void txtContact_no_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text == "")
            {
                MessageBox.Show("Please Enter UserName", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
                return;
            }
            if (cmbUserType.Text == "")
            {
                MessageBox.Show("Please Select Usertype", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbUserType.Focus();
                return;
            }
            if (txtName.Text == "")
            {
                MessageBox.Show("Please Enter Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return;
            }
            if (txtContact_no.Text == "")
            {
                MessageBox.Show("Please Enter Contact No. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtContact_no.Focus();
                return;
            }
            dbcon.dbconnection();
            dbcon.UpdateData("Update tbluserinfo set Username='" + txtUsername.Text + "', Name = '" + txtName.Text + "' ,ContactNo = '" + txtContact_no.Text + "', Email = '" + txtEmail_Address.Text + "',Usertype = '" + cmbUserType.Text + "'Where Username='" + txtuserID.Text + "';");
            MessageBox.Show("Update Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            reset();
            dbcon.DBClose(); 
        }
        void reset()
        {
            txtUsername.Text = "";
            cmbUserType.SelectedIndex = -1;
            txtContact_no.Text = "";
            txtName.Text = "";
            txtEmail_Address.Text = "";
            txtUsername.Focus();
            
        
        
        
        }
    }
}
